package controllers.administrator;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.UserGroupService;
import controllers.AbstractController;
import domain.UserGroup;
import forms.UserGroupForm;

@Controller
@RequestMapping("/userGroup/administrator")
public class UserGroupAdministratorController extends AbstractController {

	@Autowired
	private UserGroupService userGroupService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;

		Collection<UserGroup> userGroups = userGroupService.findAll();

		result = new ModelAndView("userGroup/list");
		result.addObject("userGroups", userGroups);
		result.addObject("requestURI", "userGroup/administrator/list.do");
		result.addObject("backURI", "/welcome/index.do");

		return result;

	}

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create() {
		ModelAndView result;
		UserGroupForm userGroupForm;
		Boolean isNew;

		userGroupForm = new UserGroupForm();
		isNew = true;

		result = new ModelAndView("userGroup/edit");
		result.addObject("userGroupForm", userGroupForm);
		result.addObject("requestURI", "userGroup/administrator/edit.do");
		result.addObject("isNew", isNew);
		result.addObject("backURI", "/welcome/index.do");

		return result;

	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int userGroupId) {
		ModelAndView result;
		UserGroupForm userGroupForm;
		UserGroup userGroup;
		Boolean editable;
		Boolean isNew;

		userGroup = userGroupService.findOne(userGroupId);
		userGroupForm = userGroupService.userGroupToForm(userGroup);
		editable = userGroup.getUsers().isEmpty();
		isNew = false;

		result = new ModelAndView("userGroup/edit");
		result.addObject("userGroupForm", userGroupForm);
		result.addObject("requestURI", "userGroup/administrator/edit.do");
		result.addObject("editable", editable);
		result.addObject("isNew", isNew);
		result.addObject("backURI", "/welcome/index.do");

		return result;

	}

	// Edition
	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid UserGroupForm userGroupForm,
			BindingResult binding) {
		ModelAndView result;
		UserGroup userGroup;

		if (binding.hasErrors()) {
			result = createEditModelAndView(userGroupForm);
		} else {
			try {
				if (userGroupForm.getId() == 0) {
					userGroup = userGroupService.reconstruct(userGroupForm);

				} else {
					userGroup = userGroupService.updateForm(userGroupForm);
				}

				userGroupService.save(userGroup);
				result = new ModelAndView("userGroup/administrator/list.do");

			} catch (Throwable oops) {

				result = createEditModelAndView(userGroupForm,
						"userGroup.commit.error");
			}
		}
		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "delete")
	public ModelAndView delete(@Valid UserGroupForm userGroupForm,
			BindingResult binding) {
		ModelAndView result;
		UserGroup userGroup;

		if (binding.hasErrors()) {
			result = createEditModelAndView(userGroupForm);
		} else {
			try {
			
				userGroup = userGroupService.findOne(userGroupForm.getId());

				userGroupService.delete(userGroup);
				result = new ModelAndView("userGroup/administrator/list.do");

			} catch (Throwable oops) {

				result = createEditModelAndView(userGroupForm,
						"userGroup.delete.error");
			}
		}
		return result;
	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(UserGroupForm userGroupForm) {
		ModelAndView result;

		result = createEditModelAndView(userGroupForm, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(UserGroupForm userGroupForm,
			String message) {
		ModelAndView result;

		result = new ModelAndView("userGroup/edit");

		result.addObject("userGroupForm", userGroupForm);
		result.addObject("message", message);

		return result;
	}

}
